01. Week 1: TA Sync Session¶
1.0 Introduction and the agenda¶
1.01 Purpose of the TA sync session¶
The purpose of the TA Sync session is:
- Learn implementation of the concepts using Python or otherwise
- Revise and clarifying doubts
1.02 Why Python?¶
The purpose of using Python for implementing the concepts learned:
- We start learning Python, which will be useful in subsequent subjects in this program
- We learn how to implement the concept of maths using python
- Refer to the python documentation here: https://www.python.org/doc/
- Install jupyter-notebook & Python over Anaconda environment: https://www.anaconda.com/download)
1.03 Learning methodology¶
We will use Jupyter Notebook to run the Python code snippets
We will make use of generous comments and support text
Where required, we will use graphs / charts / tabular data for better understanding
Where required, references will be shared for further studies / clarifications
In case of any questions, you are free to interrupt raise your query, no need to wait till session end
1.04 Agenda for this session¶
In this session we will cover:
Introduction to graphing
Solving linear equations involving two or more variables with variety of methods
Solving linear equations using Matrices
1.1 Introduction to Graphing¶
1.1.0 Fundamentals of graphing¶
Let us look at the below graph and answer below simple questions:
Assume that the grid lines are at unit distance
- Is the slope of this line positive or negative?
- Is the intercept positive or negative?
- What is the slope of this line?
- What is the value of intercept?
- What is the equation of this line?
More questions on lines
- How are the slopes of two parallel lines related?
- If a slope of a line is ‘m’, what will be the slope of a perpendicular line be?
1.1.1. Graphing an equation using matplotlib¶
# Let us import python package Numpy (numeric python) - for carrying out the numeric calculations
import numpy as np
# And also import the python package - Matplotlib.pyplot to plot graphs
import matplotlib.pyplot as plt
#Let us define a linear function called line0 = 2x + 1
def line0(x):
y = 2*x+1
return y
#Matplotlib documentation: https://matplotlib.org/stable/index.html
#Define a function 'graph' to plot a chart
def graph(x,f, title):
'''
This function is used to plot a line chart using matplotlib.pyplot method given the below inputs
x: Python 1d array or a list representing the value of independent variable (x)
f: Python function with an independent input variable (x)
Returns: Nothing
'''
# First of all, let us find the value of output of a given function with given inputs
y = f(x)
# Now, let us print the value of independent(x) & dependent(y) variables
print('\nx:',x)
print('\ny:',y)
# Add x-axis and y-axis lines
plt.axhline(y=0, color='green', linewidth=1)
plt.axvline(x=0, color='green', linewidth=1)
# Plot the graph
plt.plot(x, y, label=title,linewidth=2)
# Add labels and title
plt.xlabel("x")
plt.ylabel("y")
plt.title("Graph of "+title)
# Add a grid for better readability
plt.grid(True)
# Add a legend
plt.legend()
# Now, let us display the graph
plt.show()
return
# Now, let us create X array with evenly spaced values using np.linspace
# Ref: https://numpy.org/doc/stable/reference/generated/numpy.linspace.html#numpy-linspace
x = np.linspace(-10, 10, 21) # Generate range of 21 values from -10 to 10
#Now, let us see the plot using the 'graph' function
graph(x,line0,"y = 2x+1")
x: [-10. -9. -8. -7. -6. -5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.] y: [-19. -17. -15. -13. -11. -9. -7. -5. -3. -1. 1. 3. 5. 7. 9. 11. 13. 15. 17. 19. 21.]
#define another function we want to plot, now using Python's 'lambda' method
#Refer lambda function documentation: https://realpython.com/python-lambda/
line1 = lambda x: 5*x - 6 # y = 5x-6
#Now, let us see the plot using the 'graph' function
graph(x,line1,"y = 5x-6")
x: [-10. -9. -8. -7. -6. -5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.] y: [-56. -51. -46. -41. -36. -31. -26. -21. -16. -11. -6. -1. 4. 9. 14. 19. 24. 29. 34. 39. 44.]
# Let us define more complex function
parabola1 = lambda x: x**2 - 25
#Now, let us plot it as well
graph(x,parabola1,"y=x^2 - 25")
x: [-10. -9. -8. -7. -6. -5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.] y: [ 75. 56. 39. 24. 11. 0. -9. -16. -21. -24. -25. -24. -21. -16. -9. 0. 11. 24. 39. 56. 75.]
1.1.2 Graphing equation using Plotly¶
# Now, let us import more powerful plotting library package called plotly.express
import plotly.express as px
#Ref:https://www.geeksforgeeks.org/python-plotly-tutorial/
#find the value of parabola function for given range of x
y = parabola1(x)
# Plot the graph of the same x & z calculated above, using plotly
fig = px.line(x=x, y=y, title='Plotly chart for equation: y=x^2 - 25')
fig.show()
1.2 Solving linear equations involving multiple variables¶
1.2.0 Types of equation system and its impact on the solution¶
1.2.1 Solving linear equation with two variables with graphing¶
# Let us define the two functions for which we want to solve the problem using lambda method
line1 = lambda x: 2*x-1
line2 = lambda x: x+1
# Defining the input (independent) variable range
x = np.linspace(-5, 5, 21)
# Plot the graph
fig = px.line(x=x, y=[line1(x), line2(x)], title='Solving linear equation with two variables with graphing')
fig.write_html('graphing.html')
fig.show()
1.2.2 Solving parabolic + linear equation with two variables with graphing¶
# Let us define the two functions for which we want to solve the problem using lambda method
para1 = lambda x: (x**2 - 25)
line2 = lambda x: (x + 5)
# Defining the input variable array
x = np.linspace(-10, 10, 101)
# Plot the graph
fig = px.line(x=x, y=[para1(x), line2(x)])
fig.show()
1.2.3 Other methods for solving linear equation system:¶
- Names of the other methods
- What is the difference in the approach?
1.2.4 Solve linear equations using substitution method:¶
Let us take our equations as: y= m1x + c1, y= m2x + c2
Solution:
- Substituting y, we get => m1x + c1 = m2x + c2
- Solving for x => x = - (c1-c2)/(m1-m2) assuming m1!= m2
- Now, we have x, find y now => y = m1x + c1 or y = m2x + c2
# Define function to solve linear equation by using substitution method:
def solve_by_substitution1(m1,c1,m2,c2):
'''
This function solves two linear equations (expressed as y = mx + c) using the substitution method
Input m1: Slope of equation 1
c1: Intersect of equation 1
m2: Slope of equation 2
c2: Intersect of equation 1
Returns : The values of x and y
'''
x = - (c1-c2)/(m1-m2)
y = m2*x + c2
return x,y
#Call the function to solve the linear equation system with substitution system
solve_by_substitution1(-1,3, 1, -1)
(2.0, 1.0)
If the equation is in the form: a1x + b1y = d1, a2x + b2y = d2,
We will use the above function by replacing: m1 by -a1/b1, c1 by d1/b1 and likewise m2 and c2
# Define function to solve linear equation by using substitution method:
def solve_by_substitution2(a1,b1,d1,a2,b2,d2):
'''
If the equation is in the form: ax + b = d
Then we use this wrapper function over the above function
by replacing m1 by -a1/b1, c1 by d1/b1 and likewise m2 and c2
Inputs:
a1,b1,d1: Parameters for linear equation 1
a2,b2,d2: Parameters for linear equation 2
Returns: The values of x and y
'''
return solve_by_substitution1(-a1/b1, d1/b1, -a2/b2, d2/b2)
#Call the function to solve the linear equation system with substitution system
solve_by_substitution2(1,1,3,1,-1,1)
(2.0, 1.0)
1.2.5 Solve linear equations using ECHELON Method¶
This solves the linear equation by the echelon method as per steps defined below:
- Equation 1 - Multiply by a2: a1x + b1y = d1, => a1a2x + a2b1y = a2d1
- Equation 2 - Multiply by –a1: a2x + b2y = d2 => -a1a2x – a1b2y = -a1d2
- Add both to cancel x term => a2b1y - a1b2y = (a2d1 - a1d2)
- Solve for y => y = (a2d1 - a1d2) / (a2b1 - a1d2)
- Substitute y and solve for x => x = (d1 – b1y)/a1
# Let us define a function by using an echelon method
def solve_by_echelon(a1,b1,d1,a2,b2,d2):
'''
This solves the linear equation by the echelon method as per steps defined below:
- Equation 1 - Multiply by a2: a1x + b1y = d1, => a1a2x + a2b1y = a2d1
- Equation 2 - Multiply by –a1: a2x + b2y = d2 => -a1a2x – a1b2y = -a1d2
- Add both to cancel x term => a2b1y - a1b2y = (a2d1 - a1d2)
- Solve for y => y = (a2d1 - a1d2) / (a2b1 - a1d2)
- Substitute y and solve for x => x = (d1 – b1y)/a1
Inputs:
a1,b1,d1: Parameters for linear equation 1
a2,b2,d2: Parameters for linear equation 2
Returns: The values of x and y
'''
y = (a2*d1 - a1*d2)/(a2*b1 - a1*b2)
x = (d1 - b1*y)/a1
return x,y
#Call the function to solve the linear equation system with substitution system
solve_by_echelon(1,1,3,1,-1,1)
(2.0, 1.0)
1.2.6 Solving equation with two variables using Sympy library¶
#Ref: https://www.geeksforgeeks.org/python-solve-the-linear-equation-of-multiple-variable/
# importing library sympy - Python library for symbolic mathematics
from sympy import symbols, Eq, solve
# defining symbols used in equations
# or unknown variables
x, y = symbols('x,y')
# defining equations
eq1 = Eq((x+y), 3)
print("\nEquation 1:", eq1)
eq2 = Eq((x-y), 1)
print("\nEquation 2", eq2)
# solving the equation
print("\nValues of 2 unknown variable are as follows:")
print(solve((eq1, eq2), (x, y)))
display(eq1, eq2)
Equation 1: Eq(x + y, 3)
Equation 2 Eq(x - y, 1)
Values of 2 unknown variable are as follows:
{x: 2, y: 1}
1.2.7 Solving equation with multiple variables¶
# importing library sympy
from sympy import symbols, Eq, solve
# defining symbols used in equations
# or unknown variables
x, y, z = symbols('x,y,z')
# defining equations
eq1 = Eq((x+y+z), 1)
print("\nEquation 1:", eq1)
eq2 = Eq((x-y+2*z), 1)
print("\nEquation 2", eq2)
eq3 = Eq((2*x-y+2*z), 1)
print("\nEquation 3", eq3)
# solving the equation and printing the
# value of unknown variables
print("\nValues of 3 unknown variable are as follows:")
print(solve((eq1, eq2, eq3), (x, y, z)))
Equation 1: Eq(x + y + z, 1)
Equation 2 Eq(x - y + 2*z, 1)
Equation 3 Eq(2*x - y + 2*z, 1)
Values of 3 unknown variable are as follows:
{x: 0, y: 1/3, z: 2/3}
1.3 Solving linear equations using Matrices¶
1.3.1 Using augmented matrix method¶
What is the size of matrix [[1,2,3], [4,5,6]] & [[1,2], [3,4], [5,6]] ?
Question: Create augmented matrix to solve below equations:
a1x + b1y = d1 & a2x + b2y = d2 Ans: [[a1 b1 d1], [a2, b2, d2]]
Please find below how we can use the augmented matrix method and solve the linear equations using it.
#We will solve linear equations using augmented matrix method with Sympy library
#Ref:https://towardsdatascience.com/linear-algebra-systems-of-linear-equations-and-matrices-with-python-d3e0fcb29e85
#Ref:https://www.geeksforgeeks.org/python-sympy-matrix-rref-method/
#The equations are:
# 4x + 3y = 20
#-5x + 9y = 26
#Let us use the symbolic python library here
import sympy as sp
augmented_matrix = np.array([[4, 3, 20], [-5, 9, 26]])
#augmented_matrix = np.array([[1, 1, 3], [1, -1, 1]])
display(augmented_matrix)
#
#With the help of sympy.Matrix().rref() method, we can put a matrix into reduced Row echelon form.
reduced_echlon_form = sp.Matrix(augmented_matrix).rref()
reduced_echlon_form[0]
array([[ 4, 3, 20],
[-5, 9, 26]])
1.3.2 Use Numpy to solve linear equation with two variables¶
Let us solve the below equation using Matrices
Given equations: a1x + b1y = d1, a2x + b2y = d2
If A = [[a1 b1] [a2 b2]],
X = [[x] [y]] and
D = [d1 d2]
We can express above expression in a simple form as: A.X = D
Multiply both siders with A- (called A inverse): A-AX = A-B
A-A will be an identity matrix and its multiplication with X will give us X on the left hand side
So, solving it for X: X = A-1.D
#Equations to be solved
# 4x + 3y = 20
#-5x + 9y = 26
#Start with importing numpy package
import numpy as np
#The equations are:
# 4x + 3y = 20
#-5x + 9y = 26
# Define matrices to express above as AxX = B
A = np.array([[4, 3], [-5, 9]])
D = np.array([20, 26])
#Find inverse A
Ainv = np.linalg.inv(A)
print('The inverse of A is: \n', Ainv)
# Transforming the above as X = A-xB (A- means A inverse)
X = Ainv@D #@ is the operator for matrix multiplication
print('\n\n The values of the variables are:',X)
The inverse of A is: [[ 0.17647059 -0.05882353] [ 0.09803922 0.07843137]] The values of the variables are: [2. 4.]
#Let us verify if we have found the correct solution
print("Is the solution correct? Let us verify the value of B and A@X:", D, A@X)
1.3.3 Use Numpy to solve linear equation with multiple variables¶
# Now, let us solve below linear equation with three variables
# 4x + 3y + 2z = 25
# -2x + 2y + 3z = -10
# 3x -5y + 2z = -4
A = np.array([[4, 3, 2], [-2, 2, 3], [3, -5, 2]])
D = np.array([25, -10, -4])
X = np.linalg.inv(A)@D
print(X)
[ 5. 3. -2.]
1.3.4 Use Numpy .solve method to solve linear equation with multiple variables¶
# Now, let us use np.solve method to solve below linear equation with three variables
# 4x + 3y + 2z = 25
# -2x + 2y + 3z = -10
# 3x -5y + 2z = -4
A = np.array([[4, 3, 2], [-2, 2, 3], [3, -5, 2]])
D = np.array([25, -10, -4])
X = np.linalg.solve(A,D)
print(X)
[ 5. 3. -2.]
1.3.5 Use Scipy library to solve linear equation system¶
# Import the required libraries
from scipy import linalg
import numpy as np
#Scipy linear algebra: https://www.geeksforgeeks.org/scipy-linear-algebra-scipy-linalg/
# The function takes two arrays
A = np.array([[4, 3, 2], [-2, 2, 3], [3, -5, 2]])
D = np.array([25, -10, -4])
# Solving the linear equations
res = linalg.solve(A, D)
print(res)
[ 5. 3. -2.]
1.4 Solving Linear equation system with 'dependent' equations¶
import numpy as np
#Let us solve the set of linear equation system, where there are dependent equations
# 1 -> 4x + 3y + 2z = 25
# 2 -> -2x + 2y + 3z = -10
# 3 -> 2x -2y - 3z = 10
#Let us try and solve it
A = np.array([[4, 3, 2], [-2, 2, 3], [2, -2, -3]])
D = np.array([25, -10, 10])
X = np.linalg.solve(A,D)
print(X)
--------------------------------------------------------------------------- LinAlgError Traceback (most recent call last) Cell In[22], line 13 11 A = np.array([[4, 3, 2], [-2, 2, 3], [2, -2, -3]]) 12 D = np.array([25, -10, 10]) ---> 13 X = np.linalg.solve(A,D) 15 print(X) File D:\Python_R\Lib\site-packages\numpy\linalg\_linalg.py:413, in solve(a, b) 410 signature = 'DD->D' if isComplexType(t) else 'dd->d' 411 with errstate(call=_raise_linalgerror_singular, invalid='call', 412 over='ignore', divide='ignore', under='ignore'): --> 413 r = gufunc(a, b, signature=signature) 415 return wrap(r.astype(result_t, copy=False)) File D:\Python_R\Lib\site-packages\numpy\linalg\_linalg.py:104, in _raise_linalgerror_singular(err, flag) 103 def _raise_linalgerror_singular(err, flag): --> 104 raise LinAlgError("Singular matrix") LinAlgError: Singular matrix
# Since 2 & 3 are dependent equation, let us scrap 3rd equation.
# Now, there are infinite equations possible. To avoid it, let us fix the value of z and find corresponding value of x and y
#So, we write eqution3 as:
# 3 -> z = -2
#Now, let us try and solve it
A = np.array([[4, 3, 2], [-2, 2, 3], [0, 0, 1]])
D = np.array([25, -10, -2])
X = np.linalg.solve(A,D)
print(X)
[ 5. 3. -2.]
Summary and conclusion¶
Summary: We covered the below topics today:
- Introduction to graphing and drawing a line graph with matplotlib.pyplot and plotly.express
- Solving linear equation system with graphing, substitution & echelon methods
- Solving linear equation system with sympy - augmented maxtix and solve methods
- Solving linear equation system using Matrices with numpy and scipy libraries
What else we learnt:
- How to use matplotlib and plotly express package to plot graphs
- How to simplify coding efforts by creating reusable function by python's def method
- Using simple and one-line lambda function in python
- Various solutions to solving linear equations using numpy, scipy, sympy and custom defined functions
Any further questions?